package com.rq.app_base.use;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.base.rq.LOG;
import com.rq.app_base.SystemAdapter;
import com.rq.app_base.android.AndroidSystemAdapter;
import com.rq.app_base.CtrUI;
import com.rq.app_base.android.event.ClassGetter;

import java.io.Serializable;

import androidx.annotation.LayoutRes;

/**
 * @author RQ
 * date    2021-07-12 8:52
 */
public abstract class BaseController implements SystemAdapter<View, Context> {
  public static final String TAG_PASS = "controller.pass";
  public static final String TAG_PASS_DATA_SIZE = "this.size.of.pass";
  public static final String TAG_NAME = "controller";

  AndroidSystemAdapter adapter;
  Object uiFrom;

  @LayoutRes
  protected abstract int getLayoutId();

  public void setUi(CtrUI ui) {
    this.adapter = new AndroidSystemAdapter(ui);
    this.uiFrom = ui;
    this.adapter.checkUiEvent(this);
  }

  public static <C extends BaseController> Bundle getFraArguments(Class<C> clazz,
                                                                  Serializable... pass) {
    Bundle bundle = new Bundle();
    if (pass != null) {
      LOG.e("BaseController", "getFraArguments.221:TAG_PASS");
      for (int i = 0; i < pass.length; i++) {
        bundle.putSerializable(TAG_PASS + i, pass[i]);
      }
      LOG.e("BaseActivity", "getFraArguments.TAG_PASS_DATA_SIZE:" + pass.length);
      bundle.putInt(TAG_PASS_DATA_SIZE, pass.length);
    }
    bundle.putSerializable(TAG_NAME, clazz);
    return bundle;
  }

  @ClassGetter(clazz = Activity.class, name = "startActivity", param = Intent.class)
  public void open(Class<? extends BaseController> clazz, Serializable... pass) {
    Intent intent = new Intent(getContext(), BaseActivity.class);
    intent.putExtra(TAG_NAME, clazz);
    if (pass != null) {
      for (int i = 0; i < pass.length; i++) {
        intent.putExtra(TAG_PASS + i, pass[i]);
      }
      intent.putExtra(TAG_PASS_DATA_SIZE, pass.length);
    }
    adapter.send2Target(this, intent);
  }

  @ClassGetter(clazz = Activity.class, name = "startActivityForResult", param = {Intent.class, int.class})
  public void openWith(Class<? extends BaseController> clazz, int code, Serializable... pass) {
    Intent intent = new Intent(getContext(), BaseActivity.class);
    intent.putExtra(TAG_NAME, clazz);
    if (pass != null) {
      for (int i = 0; i < pass.length; i++) {
        intent.putExtra(TAG_PASS + i, pass[i]);
      }
      intent.putExtra(TAG_PASS_DATA_SIZE, pass.length);
    }
    adapter.send2Target(this, intent, code);
  }

  @ClassGetter(clazz = Activity.class, name = "finish")
  public void finishOK(Serializable... back) {
    Intent intent = new Intent();
    if (back != null) {
      for (int i = 0; i < back.length; i++) {
        intent.putExtra(TAG_PASS + i, back[i]);
      }
      intent.putExtra(TAG_PASS_DATA_SIZE, back.length);
    }
    setResult(Activity.RESULT_OK, intent);
    adapter.send2Target(this);
  }

  @ClassGetter(clazz = Activity.class, name = "finish")
  public void finish() {
    adapter.send2Target(this);
  }

  public <T extends Serializable> T getPass(int position, Intent... pass) {
    return (T) pass[0].getSerializableExtra(TAG_PASS + position);
  }


  @ClassGetter(clazz = Activity.class, name = "setResult", param = {int.class, Intent.class})
  public void setResult(int resultOk, Intent intent) {
    adapter.send2Target(this, resultOk, intent);
  }

  public void onViewCreated(Bundle savedInstanceState) {

  }

  @Override
  public int setData2View(View view, Object data) {
    return adapter.setData2View(view, data);
  }

  @Override
  public Context getContext() {
    return adapter.getContext();
  }

  @Override
  public View getView(int id) {
    return adapter.getView(id);
  }

  public void setData2View(int vId, Object obj) {
    setData2View(adapter.getView(vId), obj);
  }

  public void onResume() {
  }

  public void onPause() {
  }

  final boolean onActivityResult(int requestCode, int resultCode, Intent data) {
    if (data == null) {
      data = new Intent();
    }
    if (onResult(requestCode, resultCode, data)) {
      return true;
    }
    return false;
  }

  protected boolean onResult(int requestCode, int resultCode, Intent data) {
    return false;
  }
}
